package com.renwei.yunlong.activity.me;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.contacts.CompanyCertificateActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.FriendEnterpriseBean;
import com.renwei.yunlong.event.MinePageRefreshEvent;
import com.renwei.yunlong.global.EventKey;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 企业信息--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

@SuppressLint("NewApi")
public class EnterpriseInformationActivity extends BaseActivity implements View.OnClickListener, View.OnScrollChangeListener {
    private static final String TAG = "EnterpriseInformationActivity---:";
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_numbering)
    TextView tvNumbering;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.text_certif_status)
    TextView textCertifStatus;
    @Bind(R.id.tv_certif_status)
    TextView tvCertifStatus;
    @Bind(R.id.rl_certification)
    RelativeLayout rlCertification;
    @Bind(R.id.text_ode_next)
    TextView textOdeNext;
    @Bind(R.id.iv_code_next)
    ImageView ivCodeNext;
    @Bind(R.id.rl_code)
    RelativeLayout rlCode;
    @Bind(R.id.text_mail)
    TextView textMail;
    @Bind(R.id.tv_mail)
    TextView tvMail;
    @Bind(R.id.rl_mail)
    RelativeLayout rlMail;
    @Bind(R.id.text_contact)
    TextView textContact;
    @Bind(R.id.tv_contact)
    TextView tvContact;
    @Bind(R.id.rl_contact)
    RelativeLayout rlContact;
    @Bind(R.id.text_phone)
    TextView textPhone;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.rl_phone)
    RelativeLayout rlPhone;
    @Bind(R.id.text_documents)
    TextView textDocuments;
    @Bind(R.id.tv_documents)
    TextView tvDocuments;
    @Bind(R.id.rl_documents)
    RelativeLayout rlDocuments;
    @Bind(R.id.text_certificate_number)
    TextView textCertificateNumber;
    @Bind(R.id.tv_certificate_number)
    TextView tvCertificateNumber;
    @Bind(R.id.rl_business)
    RelativeLayout rlBusiness;
    @Bind(R.id.text_alive_years)
    TextView textAliveYears;
    @Bind(R.id.tv_alive_years)
    TextView tvAliveYears;
    @Bind(R.id.rl_alive)
    RelativeLayout rlAlive;
    @Bind(R.id.text_photo_next)
    TextView textPhotoNext;
    @Bind(R.id.rl_photo)
    RelativeLayout rlPhoto;
    @Bind(R.id.text_address)
    TextView textAddress;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.rl_address)
    RelativeLayout rlAddress;
    @Bind(R.id.text_qualification)
    TextView textQualification;
    @Bind(R.id.rl_qualification)
    RelativeLayout rlQualification;
    @Bind(R.id.comppany_scrollview)
    ScrollView comppanyScrollview;
    private String strCompanyName;


    private String companyCode = "";
    private FriendEnterpriseBean enterpriseBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enterprise_information);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        /**判断权限、管理员权限可以编辑、其他不可编辑**/
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("编辑");
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        tvTitle.setText("企业信息");
        rlCertification.setOnClickListener(this);
        rlCode.setOnClickListener(this);
        rlContact.setOnClickListener(this);
        rlPhone.setOnClickListener(this);
        rlDocuments.setOnClickListener(this);
        rlBusiness.setOnClickListener(this);
        rlPhoto.setOnClickListener(this);
        rlAlive.setOnClickListener(this);
        rlAddress.setOnClickListener(this);
        rlQualification.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            comppanyScrollview.setOnScrollChangeListener(this);
        }
    }

    private void initData() {
        refreshMcache();
        if (enterpriseBean != null && enterpriseBean.getRows() != null) {
            tvCompanyName.setText(StringUtils.value(enterpriseBean.getRows().getCompanyName()));
            strCompanyName = StringUtils.value(enterpriseBean.getRows().getCompanyName());
            tvNumbering.setText(StringUtils.value(enterpriseBean.getRows().getCompanyCode()));
            companyCode = StringUtils.value(enterpriseBean.getRows().getCompanyCode());
            if ("1".equals(enterpriseBean.getRows().getType())) {
                tvType.setText("/" + "业主");
            } else {
                tvType.setText("/" + "服务端");
            }
            //认证状态 0：未认证，1：待认证，2已认证，3未通过认证
            if ("0".equals(StringUtils.value(enterpriseBean.getRows().getCheckFlag()))) {
                tvCertifStatus.setText("未认证");
            } else if ("1".equals(StringUtils.value(enterpriseBean.getRows().getCheckFlag()))) {
                tvCertifStatus.setText("待认证");
            } else if ("2".equals(StringUtils.value(enterpriseBean.getRows().getCheckFlag()))) {
                tvCertifStatus.setText("已认证");
            } else if ("3".equals(StringUtils.value(enterpriseBean.getRows().getCheckFlag()))) {
                tvCertifStatus.setText("未通过认证");
            }
            tvContact.setText(StringUtils.value(enterpriseBean.getRows().getRegisterName()));//联系人
            tvPhone.setText(StringUtils.value(enterpriseBean.getRows().getCustomerPhone()));//电话
            tvMail.setText(StringUtils.value(enterpriseBean.getRows().getMail()));
            if ("1".equals(StringUtils.value(enterpriseBean.getRows().getCertificateType()))) {
                tvDocuments.setText("营业执照");
            } else if ("2".equals(StringUtils.value(enterpriseBean.getRows().getCertificateType()))) {
                tvDocuments.setText("组织机构代码证");
            }
            tvCertificateNumber.setText(StringUtils.value(enterpriseBean.getRows().getCertificateNumber()));//证件号码
            tvAliveYears.setText(StringUtils.value(enterpriseBean.getRows().getEffectiveAge()));
            tvAddress.setText(StringUtils.value(enterpriseBean.getRows().getProvinceId()) + StringUtils.value(enterpriseBean.getRows().getCityId()) + StringUtils.value(enterpriseBean.getRows().getRegionId()) + StringUtils.value(enterpriseBean.getRows().getRegisterAddr()));
            if ("1".equals(enterpriseBean.getRows().getType())) {//业主编辑企业信息
                if ("12".equals(ownerBean.getRows().getEmployee().getRoleId())) {
                    tvRight.setVisibility(View.VISIBLE);
                    rlCertification.setClickable(true);
                    rlCertification.setFocusable(true);
                } else {
                    tvRight.setVisibility(View.GONE);
                    rlCertification.setClickable(false);
                    rlCertification.setFocusable(false);
                }
            } else {//服务商编辑企业信息
                if ("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId())) {
                    tvRight.setVisibility(View.VISIBLE);
                    rlCertification.setClickable(true);
                    rlCertification.setFocusable(true);
                } else {
                    tvRight.setVisibility(View.GONE);
                    rlCertification.setClickable(false);
                    rlCertification.setFocusable(false);
                }
            }
        }


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit:
                startActivity(new Intent(this, EnterpriseCertificationActivity.class));
                break;
            case R.id.rl_certification://企业认证
            case R.id.tv_right:
                startActivity(new Intent(this, EnterpriseCertificationActivity.class));
                break;
            case R.id.rl_code://企业二维码
                QRCodeActivity.openActivity(this, companyCode);
                break;
            case R.id.rl_contact://联系人
                break;
            case R.id.rl_phone://手机号
                break;
            case R.id.rl_documents://证件类型
                break;
            case R.id.rl_business://证件号码
                break;
            case R.id.rl_photo://证件图片
                Intent photoIntent = new Intent(this, CompanyCertificateActivity.class);
                photoIntent.putExtra("from", "info");
                photoIntent.putExtra("certificateType", "31");
                photoIntent.putExtra("title", "证件");
                startActivity(photoIntent);
                break;
            case R.id.rl_qualification://资质证书
                Intent certificateIntent = new Intent(this, CompanyCertificateActivity.class);
                certificateIntent.putExtra("from", "info");
                certificateIntent.putExtra("certificateType", "32");
                certificateIntent.putExtra("title", "资质证书");
                startActivity(certificateIntent);
                break;

            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(MinePageRefreshEvent event) {
        LogUtil.e(TAG + "接收到相关的event ：" + event.getMessage());
        switch (event.getMessage()) {
            case EventKey.UPDATE_USER_SPECIAL:
                initData();
                break;
            case EventKey.UPDATE_COMPANY_INFO:
                initData();
                break;
            default:
                break;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        refreshCompanyInfo();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        super.onSuccess(requestId, result);
        switch (requestId) {
            case 879:
                enterpriseBean = new Gson().fromJson(result, FriendEnterpriseBean.class);
                initData();
                break;
            default:
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        int height = tvCompanyName.getMeasuredHeight() + ((View) tvCompanyName.getParent()).getTop();
        int apha = 225 / height;
        // ViewHelper.setTranslationY(tvCompanyName, 0 - scrollY);
        if (scrollY >= height) {
            tvTitle.setText(strCompanyName);
            LogUtil.e("apha：" + apha * (scrollY - height));
            if ((scrollY - height) >=height) {
                tvTitle.setTextColor(Color.argb(255, 255, 255, 255));
            } else {
                tvTitle.setTextColor(Color.argb(apha * (scrollY - height), 255, 255, 255));
            }
            TextPaint tp = tvTitle.getPaint();
            tp.setFakeBoldText(true);
            //没有透明效果
        } else {
            tvTitle.setText("企业信息");
            // ViewHelper.setTranslationY(tvCompanyName, -scrollY);
            tvTitle.setTextColor(Color.argb(255, 255, 255, 255));
            //没有透明效果
            TextPaint tp = tvTitle.getPaint();
            tp.setFakeBoldText(false);

        }
    }
}
